﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;


namespace NuaTarDoneng
{
    public partial class Article : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null||Request.QueryString["id"] =="")
            {
               
               
               // SqlDataSource2.SelectCommand = "SELECT TOP (1) * FROM blogs ORDER BY blogTime DESC";
                SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
                SqlCommand cmd = cnn.CreateCommand();
                cmd.CommandText = "SELECT TOP (1) * FROM blogs ORDER BY blogTime DESC";
                cnn.Open();
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    blogID.Value = rd["blogID"].ToString();
                    blogTitleLabel.Text = rd["blogTitle"].ToString();
                    blogContentLabel.Text = getRealContentShort(rd["blogContent"].ToString());
                   
                    
                    Timelb.Text = rd["blogTime"].ToString();
                    SortNamelb.Text = getSortNameById(rd["SortID"].ToString());
                    string desc = blogContentLabel.Text;
                    desc = (NoHTML(desc).Length > 50) ? NoHTML(desc).Substring(0, 50) : NoHTML(desc);
                    pageset.setMeta(rd["blogTitle"].ToString(), rd["keyword"].ToString()+",  " + desc, this);
                }
                cnn.Close();
            }
            else
            {
              
                
                string id = Request.QueryString["id"].ToString();
                SqlDataSource2.SelectCommand = "select * from [blogComment] where blogID='" + id + "' ";
                SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
                SqlCommand cmd = cnn.CreateCommand();
                cmd.CommandText = "select * from blogs where blogID='" + id + "'";
                cnn.Open();
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    blogID.Value = rd["blogID"].ToString();
                    blogTitleLabel.Text = rd["blogTitle"].ToString();
                    blogContentLabel.Text = getRealContentShort(rd["blogContent"].ToString());
                    Timelb.Text = rd["blogTime"].ToString();
                    SortNamelb.Text = getSortNameById(rd["SortID"].ToString());
                    string desc = blogContentLabel.Text;
                    desc = (NoHTML(desc).Length > 50) ? NoHTML(desc).Substring(0, 50) : NoHTML(desc);
                    pageset.setMeta(rd["blogTitle"].ToString(), rd["keyword"].ToString() + ",  " + desc, this);
                }
                cnn.Close();

            }
        }
        public static string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
            return Htmlstring;
        }
        protected string getRealContentShort(string content)
        {
            string trueStr = System.Text.Encoding.Default.GetString(Convert.FromBase64String(content));
            return trueStr;

        }
        protected string getSortNameById(string id)
        {
            SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandText = "select blogSort from [blogSort] where blogSortID='" + id + "'";
            cnn.Open();
            string i = Convert.ToString(cmd.ExecuteScalar());
            cnn.Close();
            return i;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
            SqlCommand cmd = cnn.CreateCommand();
            string id = Guid.NewGuid().ToString("N");
            cmd.CommandText = "insert into [blogComment] (conmentID,username,Comment,website,Email,CreatDate,blogID) values('" + id + "','" + bigname.Text + "','" + bigmessage.Text + "','" + bigweb.Text + "','" + bigemail.Text + "','" + DateTime.Now + "','"+blogID.Value+"')";
            cnn.Open();
            if (cmd.ExecuteNonQuery() == 1)
            {

                Response.Redirect(Request.Url.ToString());
            }
            cnn.Close();
        }
    }
}